package com.luxsoft.siipap.em.replica.ui.tasks;

import java.awt.event.ActionEvent;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.SwingWorker;

import com.jgoodies.binding.value.ValueHolder;
import com.jgoodies.forms.builder.ButtonBarBuilder;
import com.jgoodies.uif.AbstractDialog;
import com.jgoodies.uif.component.UIFButton;

import com.luxsoft.siipap.em.replica.importadores.ImportacionCanceledException;
import com.luxsoft.siipap.em.replica.importadores.ventas.ImportadorDeVentas;

import com.luxsoft.siipap.model.Periodo;
import com.luxsoft.siipap.swing.binding.Binder;
import com.luxsoft.siipap.util.DBUtils;
import com.luxsoft.siipap.ventas.model.Venta;

/**
 * Componente grafico para la migrar cuentas por pagar desde
 * de analisis 
 * 
 * @author Ruben Cancino
 *
 */
public class ImportadorDeVentasTask {

	public ImportadorDeVentasTask() {
		
	}
	
	public void execute(){		
		FacTaskDialog dialog=new FacTaskDialog();
		final SwingWorker worker=new Importador(dialog);
		dialog.registerWorker(worker);
		dialog.open();
		printResult();
	}
	
	protected void printResult(){
		
	}
	
	private class FacTaskDialog extends TaskDialog{
		
		protected Periodo periodo=Periodo.getPeriodoDelMesActual();
		
		private JCheckBox cuentasPorCobrarBox;
		
		public FacTaskDialog() {
			super("Importador de  Facturas (Ventas)");
		}
		
		protected JComponent buildButtonBar() {
			getHeader().setTitle("Periodo: "+periodo.toString());
			ButtonBarBuilder builder =new ButtonBarBuilder();
			builder.addGlue();
			builder.addFixed(createCreditoBtn());
			builder.addGridded(createCambioPeriodoBtn());
			builder.addUnrelatedGap();
			builder.addGridded(createStartButton(true));
			builder.addRelatedGap();
			builder.addGridded(createStopButton());
	        builder.addUnrelatedGap();
	        builder.addGridded(createDetailButton());
	        return builder.getPanel();        
		}
		
		protected JComponent createCreditoBtn(){
			cuentasPorCobrarBox=new JCheckBox("Solo Con saldo",false);
			return cuentasPorCobrarBox;
		}
		
		protected JButton createCambioPeriodoBtn(){
			Action cambiarPeriodo=new AbstractAction("Periodo"){
				public void actionPerformed(ActionEvent e) {
					if(!cuentasPorCobrarBox.isSelected()){
						ValueHolder holder=new ValueHolder(periodo);
						AbstractDialog dialog=Binder.createPeriodoSelector(holder);
						dialog.open();
						if(!dialog.hasBeenCanceled()){
							periodo=(Periodo)holder.getValue();
							getHeader().setTitle("Periodo: "+periodo.toString());
						}
						dialog.dispose();
					}
				}
			};
			UIFButton btn=new UIFButton(cambiarPeriodo);
			return btn;
		}
		
	}
	
	private class Importador extends SwingWorker<String,String>{
		
		private final FacTaskDialog dialog;
		
		public Importador(final FacTaskDialog dialog){
			this.dialog=dialog;
		}

		@Override
		protected String doInBackground() throws Exception {
			String msg=dialog.cuentasPorCobrarBox.isSelected()?"Importando todas las cuentas por cobrar":"Periodo a importar: "+dialog.periodo;
			dialog.getHeader().setTitle(msg);
			
			ImportadorDeVentas importador=new ImportadorDeVentas(){

				@Override
				public void setProgress(int progress) {
					super.setProgress(progress);
					Importador.this.setProgress(progress);
				}
				
				@Override
				public Venta importar(long analisisId) {
					if(!isCancelled())
						return super.importar(analisisId);
					else{
						dialog.infoArea.append("\nTarea cancelada");
						throw new ImportacionCanceledException("Tarea cancelada");
					}
				}
				
			};
			if(dialog.cuentasPorCobrarBox.isSelected())
				importador.importarCarteraDeCredito();
			else
				importador.importar(dialog.periodo,10);
			return "OK";
		}

		@Override
		protected void process(List<String> chunks) {
			for(String s:chunks){
				dialog.infoArea.append(s+"\n");
			}
		}

		@Override
		protected void done() {
			dialog.progress.setValue(100);
			super.done();
		}

		
		
	}
	
	public static void main(String[] args) {
		DBUtils.whereWeAre();
		new ImportadorDeVentasTask().execute();
	}

}
